<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="cn.firstsoft.firstframe.admin.PropertyFile"%>
<%@ page import="cn.firstsoft.firstframe.admin.XmlFile"%>
<%@ page import="cn.firstsoft.firstframe.admin.Field"%>
<%@ page import="cn.firstsoft.firstframe.admin.InputValue"%>

<%@ include file="../common.jsp"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title></title>
  </head>
  
  <body>
  	
    <%	
    	
		String method = request.getParameter("method")==null?"":request.getParameter("method");		
    	String pname = request.getParameter("pname")==null?"":request.getParameter("pname");   
    	
    	if(method.equals("del"))
	    {
	    	XmlFile.removeContent(pname);
	    	response.sendRedirect("field_list.jsp");
	    	return;
	    }
    	String tmp_type = request.getParameter("type")==null?"":request.getParameter("type");
    	String[] typeAry = tmp_type.split(",");
    	String type = typeAry[0];    	
    	String tmp_typeCname = typeAry[1];
    	String typeCname = new String(tmp_typeCname.getBytes("iso-8859-1"),"utf-8"); 	
    	
    	String inputType = request.getParameter("inputtype")==null?"":request.getParameter("inputtype");	
		String name = request.getParameter("name")==null?"":request.getParameter("name");
		
		String tmpCname = request.getParameter("cname")==null?"":request.getParameter("cname");	
		String cname=new String(tmpCname.getBytes("iso-8859-1"),"utf-8");
		
		String inputSize = request.getParameter("inputSize")==null?"":request.getParameter("inputSize");
		String tmp_valueName = request.getParameter("valueName")==null?"":request.getParameter("valueName");
		String valueName = new String(tmp_valueName.getBytes("iso-8859-1"),"utf-8");

		String tmp_inputValues = request.getParameter("inputValues")==null?"":request.getParameter("inputValues");
		String inputValues = new String(tmp_inputValues.getBytes("iso-8859-1"),"utf-8");
		
		String tmp_defaultValue = request.getParameter("defaultValue")==null?"":request.getParameter("defaultValue");
		String defaultValue = new String(tmp_defaultValue.getBytes("iso-8859-1"),"utf-8");
		
		String tmp_desc = request.getParameter("desc")==null?"":request.getParameter("desc");
		String desc = new String(tmp_desc.getBytes("iso-8859-1"),"utf-8");
		
		String tmp_alertField = request.getParameter("alertField")==null?"":request.getParameter("alertField");
		String alertField = new String(tmp_alertField.getBytes("iso-8859-1"),"utf-8");
		
		String rules = request.getParameter("rules")==null?"":request.getParameter("rules");
		String pos = request.getParameter("pos")==null?"":request.getParameter("pos");
		Field field = new Field();
		field.setType(type);
		field.setTypeCname(typeCname);	
		field.setPname(pname);		
		field.setCname(cname);
		field.setName(name);
		field.setInputType(inputType);
		if(!inputType.equals("text"))
			field.setInputSize(new Integer(0));
		else
			field.setInputSize(new Integer(inputSize));
		field.setDefaultValue(defaultValue);
		field.setDesc(desc);
		field.setValidatorAlertField(alertField);
		field.setInputValidator(rules);
		field.setPos(new Integer(pos));
		String[] valueNameAry = valueName.split(",");
		String[] inputValuesAry = inputValues.split(",");
		List inputValueList = new ArrayList();		
		for(int i=0;i<valueNameAry.length;i++)
		{
			InputValue iv = new InputValue();
			iv.setName(valueNameAry[i]);
			iv.setValue(inputValuesAry[i]);
			inputValueList.add(iv);
		}
	
		field.setInputValues(inputValueList);
		
		boolean flag = false;
    		if(method.equals("add"))
    		{  			
    			if(XmlFile.isExites(pname))
		    	{
	    			response.sendRedirect("field_edit.jsp?flag=exists&method=add&pname="+pname);	 
	    			return;				
	    		}
	    		flag = XmlFile.addContent(field);
	    		
	    		if(flag)
	    		{
	    			response.sendRedirect("field_list.jsp?flag=suc");	 
	    			return;
	    		}		
	    	
	    		else{
	    			response.sendRedirect("field_list.jsp?flag=err");	  
	    			return;
	    		}
	    	}else if(method.equals("edit")){
	    		flag = XmlFile.modifyContent(field);
	    	}
			if(flag)
    			response.sendRedirect("field_list.jsp?flag=suc");	 
    		else
    			response.sendRedirect("field_list.jsp?flag=err");
    %>
   
    
  </body>
</html>